package com.wobito.service.impl;

import com.wobito.common.constants.BusinessException;
import com.wobito.common.constants.ErrorCode;
import com.wobito.mapper.DbMapper;
import com.wobito.mapper.TableMapper;
import com.wobito.pojo.bean.Table;
import com.wobito.service.DbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lambyang
 * @since 2024-09-19
 */
@Slf4j
@Service
public class DbServiceImpl implements DbService {

    @Autowired
    private DbMapper dbMapper;
    @Autowired
    private TableMapper tableMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createNewTable(String tableInfo) {
        try {
            //先新增表名表
            tableMapper.insert(new Table(tableInfo));
            //根据表名创建每月表
            dbMapper.createNewOperateLog("t_operate_log_" + tableInfo);
            dbMapper.createNewDeviceLog("t_device_log_" + tableInfo);
        } catch (Exception e) {
            log.error("创建表错误:"+e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"创建表失败!");
        }
    }
}
